//
//  ChooseCityInfoView.m
//  Cycling
//
//  Created by CSS on 5/18/23.
//

#import "ChooseCityInfoView.h"

@interface ChooseCityInfoView ()

@property (nonatomic) NSInteger selectProvince;
@property (nonatomic) NSInteger selectCity;
@property (nonatomic, strong) NSArray *citysOnProvince;

@end

@implementation ChooseCityInfoView

+ (ChooseCityInfoView *)chooseAddressWith:(UIViewController *)vc andDataArr:(NSArray *)dataArr complection:(void (^)(NSString *))block {
    
    ChooseCityInfoView *chooseView = [[ChooseCityInfoView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 56, 373)];
    chooseView.dataArr = dataArr;
    chooseView.selectProvince = 0;
    chooseView.selectCity = 0;
    chooseView.citysOnProvince = [dataArr[0] valueForKey:@"children"];
    
    [chooseView initUI];
    chooseView.complectionBlock = block;
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentLikeAlertView:chooseView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = NO;
    
    return chooseView;
}

- (void)sure {
    if(self.dataArr.count <= self.selectProvince || self.citysOnProvince.count <= self.selectCity) {
        return;
    }
    NSString *province = [self.dataArr[self.selectProvince] valueForKey:@"text"];
    if(province == nil) {
        province = @"";
    }
    NSString *city = [self.citysOnProvince[self.selectCity] valueForKey:@"text"];
    if(city == nil) {
        city = @"";
    }
    if(self.complectionBlock) {
        self.complectionBlock([NSString stringWithFormat:@"%@,%@", city, province]);
    }
    
}

- (void)cancle {
    if(self.complectionBlock) {
        self.complectionBlock(@"");
    }
}

- (void)initUI {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W - 56, 373)];
    view.userInteractionEnabled = YES;
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 16;
    view.layer.masksToBounds = YES;
    view.center = self.center;
    [self addSubview:view];
    
    UIButton *cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancleBtn setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    cancleBtn.userInteractionEnabled = YES;
    [view addSubview:cancleBtn];
    [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(view.mas_right);
        make.top.equalTo(view.mas_top).offset(5);
        make.width.offset(58);
        make.height.offset(58);
    }];
    
    UILabel *titleL = [[UILabel alloc] init];
    titleL.text = CycLocalizedString(@"Chose your city", @"请选择您所在的城市");
    titleL.textColor = COLOR_TOPIC_SELECT;
    titleL.font = [UIFont systemFontOfSize:26];
    titleL.textAlignment = NSTextAlignmentCenter;
    [view addSubview:titleL];
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(view.mas_top).offset(43);
            make.centerX.equalTo(self.mas_centerX);
    }];
        
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setBackgroundImage:[UIImage imageNamed:@"btn_login"] forState:UIControlStateNormal];
    [submitBtn setTitle:CycLocalizedString(@"Submit", @"提交") forState:UIControlStateNormal];
    [submitBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view.mas_left).offset(29);
        make.right.equalTo(view.mas_right).offset(-29);
        make.bottom.equalTo(view.mas_bottom).offset(-28);
        make.height.offset(54);
    }];
    
    self.datePicker=[[UIPickerView alloc]init];
//    if (@available(iOS 13.4, *)) {
//        self.datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
//    }
    //frame得在设置样式之后设置,否者宽度位置显示会有问题
    [self.datePicker setFrame:CGRectMake(30, 100, view.ce_width - 60, 180)];
    self.datePicker.delegate = self;
    self.datePicker.dataSource = self;
    [self.datePicker reloadAllComponents];
    
//    [self.datePicker setDatePickerMode:UIDatePickerModeDate];
//    [self datePickerSetUp];
    [self addSubview:self.datePicker];

}

- (NSInteger)numberOfComponentsInPickerView:(nonnull UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if(component == 0) {
        return self.dataArr.count;
    }
    return self.citysOnProvince.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == 0) {
        if(self.dataArr.count <= row) {
            return @"";
        }
        return [self.dataArr[row] valueForKey:@"text"];
    }
    if(self.citysOnProvince.count <= row) {
        return @"";
    }
    return [self.citysOnProvince[row] valueForKey:@"text"];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0) {
        self.selectProvince = row;
        self.selectCity = 0;
        self.citysOnProvince = [self.dataArr[row] valueForKey:@"children"];
        [pickerView reloadComponent:1];
        [pickerView selectRow:self.selectCity inComponent:1 animated:YES];
    } else {
        self.selectCity = row;
    }
}

@end
